package com.hepinghospital.user.ui.activity.eyecheck;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.decode.ImageAttrs;
import me.panpf.sketch.request.CancelCause;
import me.panpf.sketch.request.DisplayListener;
import me.panpf.sketch.request.ErrorCause;
import me.panpf.sketch.request.ImageFrom;

/**
 * @author Administrator
 * @date 2018/6/8
 */

public class BigImageActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.scaleview)
    SketchImageView scaleview;

    String url;
    int type;

    public ProgressDialog dialog;

    @Override
    protected void initViews() {
        tvTitle.setText("查看详情");
        url = getIntent().getStringExtra("url");
        type = getIntent().getIntExtra("type", 0);
        scaleview.setOnLongClickListener(v -> {
            scaleview.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(scaleview.getDrawingCache());
            scaleview.setDrawingCacheEnabled(false);
            saveBitmap(bitmap);
            return false;
        });
        scaleview.setZoomEnabled(true);
        scaleview.setDisplayListener(new DisplayListener() {
            @Override
            public void onStarted() {

            }

            @Override
            public void onCompleted(@NonNull Drawable drawable, @NonNull ImageFrom imageFrom, @NonNull ImageAttrs imageAttrs) {
                dialog.dismiss();
            }

            @Override
            public void onError(@NonNull ErrorCause cause) {

            }

            @Override
            public void onCanceled(@NonNull CancelCause cause) {

            }
        });
        if (type == 1) {
            scaleview.displayImage(url);
        } else if (type == 2) {
            scaleview.displayImage(url);
        }
    }

    @Override
    protected void initData() {
        dialog = ProgressDialog.show(BigImageActivity.this, "提示", "加载中…", true, false, null);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_big_image;
    }


    public void saveBitmap(Bitmap bitmap) {
        try {
            // 获取内置SD卡路径
            String sdCardPath = Environment.getExternalStorageDirectory().getPath();
            @SuppressLint("SimpleDateFormat") String imgNameOri = "HomePic_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            // 图片文件路径
//            File file = new File(sdCardPath);
//            File[] files = file.listFiles();
//            for (int i = 0; i < files.length; i++) {
//                File file1 = files[i];
//                String name = file1.getName();
//            }
            String filePath = sdCardPath + "/" + imgNameOri + ".png";
            File file = new File(filePath);
            FileOutputStream os = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.flush();
            os.close();

            //把文件插入到系统图库
            MediaStore.Images.Media.insertImage(getContentResolver(),
                    file.getAbsolutePath(), imgNameOri + ".png", null);

            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(file);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

            ToastUtil.showToast(mContext, "保存成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
